import sys

import os
import ConfigParser

class RBSConfig:
    
    def __init__(self, configFile="/etc/rbs.cfg"):
        """
        RBS Configuration file setup
        """
        
        # Validate if config file exists and can be read
        #if os.path.isfile(configFile):
        #    if os.access(configFile, os.R_OK):
        #        self.configFile = configFile
        #    else:
        #        raise Exception("Configuration file/path not accessible or readable by the uid/gid running this application.  Try again.")
        #else:
        #    raise Exception("Configuration file is not a file or does not exist.  Try again.")

        # Configuration file
        self.configFile = configFile

        # Setup ConfigParser object
        self.parserConfig = ConfigParser.ConfigParser()
        
        # Try to read/load configuration file
        self.loadConfiguration()

    def loadConfiguration(self):
        """
        Load or Re-load RBS Configuratoin
        """
        self.parserConfig.read(self.configFile)

    def getConfiguration(self, section):
        """
        Get and parse RBS configuration
        """
        config = {}
    
        options = self.parserConfig.options(section)
        
        for option in options:
            try:
                if "," in self.parserConfig.get(section, option):
                    config[option] = self.parserConfig.get(section, option).split(",")
                else:
                    config[option] = self.parserConfig.get(section, option)
            except:
                config[option] = None
    
        return config